import http from "@/utils/http";
/**
*add by wuyx 销售品实例明细回显
*/
const pageOfferInstDetail = {


    /**
     * 销售品实例明细回显主函数
     * @param {*} data 实例数据
     * @param {*} objArr formData 数据项
     * @param {*} mainProdId 主产品ID
     * @returns 
     */
    RewObject: (data, objArr,mainProdId) => {//明细接口回显的数据，formdata,productId
        
        
        let orderDetails = data.resultObject;
        for (let key in objArr) {
            let item = objArr[key];
                if(orderDetails[item.tableName]==undefined&&key!='ORDER__STAFF_INFO'){
                    continue;
                }       
                if (item.tableName == 'CUSTOMER_ORDER') {
                    orderDetails[item.tableName].forEach(m => {
                        if(item.colId=="handleType"){
                            item.value=""+pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue=""+pageOfferInstDetail.getVal(m[item.colId]);
                        }else {
                            item.value=pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                        }
                    });
                }
                else if (item.tableName == 'ORD_CUSTOMER') {
                    orderDetails[item.tableName].forEach(m => {
                        item.value= pageOfferInstDetail.getVal(m[item.colId]);
                        item.oldValue=pageOfferInstDetail.getVal(m[item.colId]);
                    });
                }
                else if (item.tableName == 'CUSTOMER') {
                    orderDetails[item.tableName].forEach(m => {
                            item.value= pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue=pageOfferInstDetail.getVal(m[item.colId]);                  

                    });
                }
                else if (item.tableName == 'CUST_WORK'&&orderDetails[item.tableName]!=undefined) {
                    orderDetails[item.tableName].forEach(m => {
                            item.value= pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue=pageOfferInstDetail.getVal(m[item.colId]);                  

                    });
                }
                else if (item.tableName == 'ORD_DEV_STAFF_INFO') {
                    orderDetails['DEV_STAFF_INFO'].forEach(m => {
                        if(item.text!=undefined&& m[item.colId]!=''&&item.devStaffType==m['devStaffType']){
                            item.value= pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue=pageOfferInstDetail.getVal(m[item.colId]);  
                            item.text=pageOfferInstDetail.getVal(m[item.colId]); 
                        }
               

                    });
                }
                //发展人插件赋值
                else if (key == 'ORDER__STAFF_INFO') {
                    orderDetails['DEV_STAFF_INFO'].forEach(m => {
                        pageOfferInstDetail.setMutilVal(item,'value',pageOfferInstDetail.getVal(m['devStaffId']));
                        pageOfferInstDetail.setMutilVal(item,'oldValue',pageOfferInstDetail.getVal(m['devStaffId']));
                        pageOfferInstDetail.setMutilVal(item,'text',pageOfferInstDetail.getVal(m['devStaffId']));
                     });
                }
                else if (item.tableName == 'ORDER_ITEM') {
                    let orderItem=pageOfferInstDetail.getOrderItemByMainProdId(orderDetails[item.tableName],mainProdId)
                    
                    if(orderItem!=undefined&&orderItem!=null){
                        item.value = pageOfferInstDetail.getVal(orderItem[item.colId]);
                        item.oldValue=pageOfferInstDetail.getVal(orderItem[item.colId]);
                    }
                }
                else if (item.tableName == 'ORDER_ITEM_ATTR') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.attrId == item.colId) {
                            item.value = m.attrValue;
                            item.oldValue=m.attrValue;
                        }
                    });
                }
                else if (item.tableName == 'PROD_INST') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.roleCd == item.attrRoleCd) {
                            if(item.colId=='lanId'){
                                item.value =pageOfferInstDetail.getVal(m['locName']); 
                                item.oldValue= pageOfferInstDetail.getVal(m['locName']);
                            }else{
                                item.value =pageOfferInstDetail.getVal(m[item.colId]);
                                item.oldValue=pageOfferInstDetail.getVal(m[item.colId]);
                            }
                            if(item.colId=='useCustId'){
                                item.value=pageOfferInstDetail.getVal(m[item.colId]);
                                item.text=pageOfferInstDetail.getVal(m['useCustName']);
                            }
                                                                               
                        }
                        if (m.roleCd == 0 && !item.attrRoleCd) {
                            if(item.colId=='lanId'){
                                item.value =pageOfferInstDetail.getVal(m['locName']); 
                                item.oldValue= pageOfferInstDetail.getVal(m['locName']);
                            } else if(item.colId=='payObjId'&&m['payObjName']!=undefined){
                                item.value =pageOfferInstDetail.getVal(m['payObjName']); 
                                item.oldValue= pageOfferInstDetail.getVal(m['payObjName']);
                                //付费对象label动态显示
                                let payObj=document.getElementById(key);
                                if(payObj!=undefined&&payObj!=null){
                                    let lables=payObj.getElementsByTagName("label");
                                    let payType=m['payType']
                                    if(payType=='30010001'){
                                        lables[0].innerHTML='付费客户';
                                    }else{
                                        lables[0].innerHTML='付费运营商';
                                    }
                                }
              

                            }else if(item.id=='141000400'){
                                let pordInst100=pageOfferInstDetail.getDataByRoleCd(orderDetails['PROD_INST'],100);
                                item.value =pageOfferInstDetail.getVal(pordInst100['prodInstId']);
                                item.oldValue=pageOfferInstDetail.getVal(pordInst100['prodInstId']);
                            }else if(item.id=='141000401'){
                                let pordInst200=pageOfferInstDetail.getDataByRoleCd(orderDetails['PROD_INST'],200);
                                item.value =pageOfferInstDetail.getVal(pordInst200['prodInstId']);
                                item.oldValue=pageOfferInstDetail.getVal(pordInst200['prodInstId']);
                            }else{
                                item.value = pageOfferInstDetail.getVal(m[item.colId]);  
                                item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);  
                            }
                        } 
                                       
                    });

                }
                else if (item.tableName == 'PROD_INST_ATTR') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.roleCd == item.attrRoleCd&&m.attrId==item.colId) {
                           if(item.text!=undefined){
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                                item.text = m['attrValueName']==undefined?m.attrValue:m['attrValueName'];
                            }else{
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                            }                                                                        
                        }
                        if (m.roleCd == 0 && !item.attrRoleCd&&m.attrId==item.colId) {
                           if(item.text!=undefined){
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                                item.text = m['attrValueName']==undefined?m.attrValue:m['attrValueName'];
                            } else{
                                item.value =m.attrValue; 
                                item.oldValue=m.attrValue; 
                            }  
                        }  
                                       
                    });
                }
                else if (item.tableName == 'ORDER_TASK') {
                    orderDetails[item.tableName].forEach(m => {
                        item.value= pageOfferInstDetail.getVal(m[item.colId]);
                        item.oldValue=pageOfferInstDetail.getVal(m[item.colId]);   
                        
                    });
                }
                else if (item.tableName == 'ORDER_TASK_ATTR'&&orderDetails[item.tableName]!=undefined) {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.attrId == item.colId) {
                            item.value = m.attrValue;
                            item.oldValue=m.attrValue; 
                        }
                    });
                }
                else if (item.tableName == 'PROD_INST_CONTACT') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.contactType == "6" && item.attrRoleCd == "100") {
                            item.value = pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                        }
                        if (m.contactType == "7" && item.attrRoleCd == "200") {
                            item.value = pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                        }
                        if (m.contactType == "1" && (item.attrRoleCd == "0"||!item.attrRoleCd)) {
                            item.value = pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                        }
                        
                    });
                }
                else if (item.tableName == 'ORDER_ITEM_FILE') {
                    orderDetails[item.tableName].forEach(m => {
                        item.value = pageOfferInstDetail.getVal(m[item.colId]);
                        item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                    });
                }
                else if (item.tableName == 'ACCT_ITEM') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.acctTypeCd== item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferInstDetail.getVal(m.amount);
                                    item.oldValue= pageOfferInstDetail.getVal(m.amount);
                                }
                                pageOfferInstDetail.setAmoutRelattr(item,orderDetails,objArr);
                            }else{
                                item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                            }
                            
                        }
                        if ((m.acctTypeCd == 0) && !item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferInstDetail.getVal(m.amount);
                                    item.oldValue= pageOfferInstDetail.getVal(m.amount);
                                }
                                pageOfferInstDetail.setAmoutRelattr(item,orderDetails,objArr);
                            }else{
                                item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                            }     
                        }
                        if(!item.attrRoleCd){    
                            item.value = pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferInstDetail.getVal(m[item.colId]); 
                        }
                    });
                }
                else if (item.tableName == 'ACCT_ITEM_ORG') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.chargeOrgCd== item.attrRoleCd) {
                            if(item.text !=undefined && m[item.colId]!=''){
                                item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                                let colName=item.colId.replace('Code', 'Name');
                                item.text = m[colName];
                            }else{
                                item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                            }
                        }
                        else if ((m.chargeOrgCd == 0) && !item.attrRoleCd) {
                           item.value = pageOfferInstDetail.getVal(m[item.colId]);
                           item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                        }
                        if(!item.attrRoleCd){
                            item.value =''+ pageOfferInstDetail.getVal(m[item.colId]);
                            item.oldValue=''+ pageOfferInstDetail.getVal(m[item.colId]);
                        }

                    });
                }
                else if (item.tableName == 'OFFER_INST_FEE_INFO') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.roleCd == item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferInstDetail.getVal(m.amount);
                                    item.oldValue= pageOfferInstDetail.getVal(m.amount);
                                    pageOfferInstDetail.setAmoutRelattr(item,orderDetails,objArr);
                                }                              
                            }else{
                               // 
                                // item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                // item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                                // item.instId=m['offerInstFeeInfoId'];
                                // item.rowId=m['rowId'];
                                
                            }
        
                        }
                        else if ((m.roleCd == 0) && !item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferInstDetail.getVal(m.amount);
                                    item.oldValue= pageOfferInstDetail.getVal(m.amount);
                                }
                                pageOfferInstDetail.setAmoutRelattr(item,orderDetails,objArr);
                            }          
                        }
                        // if(!item.attrRoleCd){    
                        //     item.value = m[item.colId];                                                    
                        // }
                    });
                }
                if (item.tableName == 'PROD_INST_ACCT_REL') {
                    orderDetails[item.tableName].forEach(m => {                   
                        if(!item.attrRoleCd&&item.colId=='acctId'&& m['acctName']!=undefined){    
                            item.value = pageOfferInstDetail.getVal(m['acctName']);
                            item.oldValue= pageOfferInstDetail.getVal(m['acctName']);                                     
                        }
                    });
                }

                else if (item.tableName == 'OFFER_INST_FEE_ATTR') {
                    orderDetails[item.tableName].forEach(m => {
                        if (m.attrId == item.colId) {
                            if(item.text!=undefined){
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                                item.text = m['attrValueName'];
                            }else{
                                item.value = m.attrValue;
                                item.oldValue=m.attrValue;
                            }
                        }
                    });
                }
          
        }
        
        return objArr;
    },
    /**
     * 获取主产品订单项
     * @param {*} orderItems 
     * @param {*} mainProdId 
     * @returns 
     */
    getOrderItemByMainProdId:(orderItems,mainProdId)=>{
       let orderItem;
       orderItems.forEach(i=>{
           if(i.applyObjSpec==mainProdId){
            orderItem=i;
            return;
           }
       })
       return orderItem;
    },
    /**
     * 暂时不用
     * @param {*} prodAttrAll 
     * @param {*} prodInstId 
     * @returns 
     */
    getProdAttrByProdInstId:(prodAttrAll,prodInstId)=>{
        let prodAttrs=[];
        prodAttrAll.forEach(a=>{
            if(a.prodInstId==prodInstId){
                prodAttrs.push(a);
            }
        })
        return prodAttrs;

    },
    /**
     * 暂时不用
     * @param {*} res 
     * @param {*} RoleCd 
     * @param {*} data 
     * @param {*} tableName 
     * @param {*} mountTableName 
     */
    setInstAcctRel: (res, RoleCd,data,tableName,mountTableName) => {
        let rowData=pageOfferInstDetail.getDataByRoleCd(data[tableName],RoleCd);
        if(rowData){
            let intAcctAttr = res.filter((item) => {
                return item.tableName == tableName && item.colId !== 'amount'
            })
    
            let newMount = res.filter((item) => {
                return item.tableName == mountTableName && item.colId == 'amount'
            })
    
            newMount.forEach((item) => {            
                let mapArr = [];
                
                if (item.attrRoleCd == RoleCd) {
                    if(item.relIds){
                        mapArr = item.relIds.split(";")
                    }
                    mapArr.forEach((mapAttrItem)=>{
                        intAcctAttr.forEach((acctAttrItem)=>{
                            let id=acctAttrItem.id;
                            if(mapAttrItem.includes(id)){
                                acctAttrItem.value= rowData[acctAttrItem.colId];
                            }
    
                        })
                    })
                }
            })
        }
        
    },
    /**
     * 根据rolecd获取实例数据
     * @param {*} data 
     * @param {*} RoleCd 
     * @returns 
     */
    getDataByRoleCd:(data,RoleCd)=>{
        let rowData;
        data.forEach(i=>{
            if(i.roleCd==RoleCd){
               rowData=i;
               return;
            }
        })
        return rowData;
    },
    /**
     * 去除未定义字段属性
     * @param {*} val 
     * @returns 
     */
    getVal:(val)=>{
        if(val!=undefined){
            return val;
        }else{
            return '';
        }
    },
    setMutilVal:(item,filed,val)=>{
        if(item[filed]&&item[filed]!=''){
            let vals=item[filed].split(",");
            if(!vals.includes(val)){
                item[filed]=item[filed]+","+val;
            }
        }else{
            item[filed]=""+val;
        }
    },
    /**
     * 资费信息根据relIds设置关联属性信息
     * @param {*} amoutItem 
     * @param {*} orderDetail 
     * @param {*} objArr 
     */
    setAmoutRelattr:(amoutItem,orderDetail, objArr)=>{
      let relIds=amoutItem.relIds;
      if(relIds!=undefined&&relIds!=""){
        let mapArr = relIds.split(";")
        mapArr.forEach(attr=>{
            for (let key in objArr) {
                let item = objArr[key];
                if(attr==item.id&&orderDetail[item.tableName]!=undefined){
                    orderDetail[item.tableName].forEach(m => {
                        if((amoutItem.acctItemId==m.acctItemTypeId||amoutItem.acctItemId==m.acctItemGroupId)&&m[item.colId]!=undefined){
                            if(item.attrRoleCd==m.roleCd){
                                if(item.text !=undefined){
                                    item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                    item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                                    let colName=item.colId+ 'Name';
                                    item.text = pageOfferInstDetail.getVal(m[colName]);
                                }else{
                                    item.value = pageOfferInstDetail.getVal(m[item.colId]);
                                    item.oldValue= pageOfferInstDetail.getVal(m[item.colId]);
                                }                              
                            } else if(!item.attrRoleCd) {                              
                                if(item.text !=undefined){
                                    item.value = m[item.colId];
                                    item.oldValue=m[item.colId];
                                    let colName=item.colId+ 'Name';
                                    item.text = m[colName];
                                }else{
                                    item.value = m[item.colId];
                                    item.oldValue=m[item.colId];
                                }                              
                            }                       
                           

    

                        }
                    })
                }
            }
        })

      }
    },
    	/**
		 * 查询产品实例明细数据
         * {"custId":"2061","prodInstId":"57003","offerInstId":"28918","custSoNumber":"AHHF202100000464","orderItemIds":"151379"}
		 * @param {*} condition 
		 * @returns 
		 */
		async qryPageOfferInstDetail(condition) {
			let res = await http({
				url: `${process.env.VUE_APP_CTX_ORDER_QUERY}/qryOfferInstDetailConvertUnderLine`,
				data: condition,
				method: "POST",
			});
			return res;
		}



};

export default pageOfferInstDetail;
